package cn.ztuo.bitrade.controller;

import cn.ztuo.bitrade.QRCode.QRCodeUtils;
import cn.ztuo.bitrade.QRCode.SamplePoster;
import cn.ztuo.bitrade.entity.ContractBond;
import cn.ztuo.bitrade.service.ContractBondService;
import cn.ztuo.bitrade.service.ContractOrderService;
import cn.ztuo.bitrade.util.ContractUtils;
import cn.ztuo.bitrade.util.MessageResult;
import com.quaint.poster.core.impl.PosterDefaultImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.*;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 获取合约所有的杠杆
 */
@RequestMapping("/contract-bond")
@RestController
public class ContractBondController {
    @Autowired
    private ContractBondService bondService;
    @Autowired
    private ContractOrderService contractOrderService;


    @PostMapping("/getlistlocation")
    public  MessageResult calculation(String symbol){
        MessageResult mr = new MessageResult(0,"success");
        Sort sort = new Sort(Sort.Direction.DESC,"multiple");
        List<ContractBond> byAll =  bondService.findBySymbol(symbol,sort);
        byAll=byAll.stream().sorted(Comparator.comparing(a->Integer.parseInt(a.getMultiple()))).collect(Collectors.toList());
        mr.setData(byAll);
        return mr;
    }

    /**
     * 根据杠杆 得到对应的保证金
     * @param symbol
     * @param lever
     * @param amount
     * @param price
     * @return
     */
    @PostMapping("/getlever")
    public MessageResult getlever(String symbol, BigDecimal lever, BigDecimal amount, BigDecimal price){
        MessageResult mr = new MessageResult(0,"success");
        ContractBond bySymbolAndMultiple = bondService.findBySymbolAndMultiple(symbol,lever);
        BigDecimal initialMargin = bySymbolAndMultiple.getInitialMargin();
        BigDecimal getbond = ContractUtils.getbond(amount, price, initialMargin);
        mr.setData(getbond);
        return mr;
    }
    @RequestMapping("/test")
    public MessageResult test(HttpServletRequest req, HttpServletResponse res) throws Exception {
        InputStream qrImgInuptStream = QRCodeUtils.getQRImgInuptStream(null);
        // 测试注解, 这里读取图片如果不成功，请查看target 或者 out 目录下是否加载了资源。 如需使用，请引入spring core依赖
        BufferedImage background = ImageIO.read(new ClassPathResource("image/yayi.png").getInputStream());
        BufferedImage head = ImageIO.read(qrImgInuptStream);
        SamplePoster poster = SamplePoster.builder()
                .backgroundImage(background)
                .head(head)
                .nickName("Quaint")
                .slogan("命运多舛，痴迷淡然。挥别了青春，数不尽的车站。甘于平凡，却不甘平凡地溃败。")
                .mainImage(head)
                .build();
        PosterDefaultImpl<SamplePoster> impl = new PosterDefaultImpl<>();
        BufferedImage test = impl.annotationDrawPoster(poster).draw(null);


        ByteArrayOutputStream bs = new ByteArrayOutputStream();
        ImageOutputStream imOut = ImageIO.createImageOutputStream(bs);
        ImageIO.write(test, "png", imOut);
        InputStream is = new ByteArrayInputStream(bs.toByteArray());
        OutputStream out = res.getOutputStream();
        byte [] by = new byte[1024];
        int i = 0;
        while((i=is.read(by))!=-1)
        {
            out.write(by,0,i);
        }
        is.close();
        return MessageResult.success("");
    }



}
